function   [credit_spread, model_sol] = post_processing_define_spreads( file_path, par, S )
% Usage: post_processing_define_spreads( file_path, par, S ). 
% This file generates credit spread by reading the file from file_path, and then save it back with the added credit
% spread. 

if(nargin<3)
    S = load( file_path );
end

solve_credit_spread_again = isfield( par.alg_control, 'solve_credit_spread_again' ) && par.alg_control.solve_credit_spread_again;   % if we want to solve again even if the file contains the solution for credit spread.

if( ~solve_credit_spread_again  && isfield(S.model_sol, 'credit_spread'))
    credit_spread = S.model_sol.credit_spread;
    return;   % If it has already been defined and we do not want to sovle everything again. 
end
disp( ['>>>>> Solving the credit spreads for ', file_path ] )

model_sol =  post_processing(S.model_sol);

% Solve for the credit spreads.
results   =  solve_v(  model_sol,  par  );  %solve the prices of safe and risky bonds, as well as the credit spread, defined as the yield spreads between the two types of bonds.
credit_spread = results.credit_spread;

% Add this new credit spread to the file.
model_sol = S.model_sol;
model_sol.credit_spread = credit_spread;

if( strcmp( file_path, '' ) ~=1 )
    save( file_path,    'model_sol'  );
end


